/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.news.service.impl;

import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.news.domain.YxNews;
import co.yixiang.modules.news.service.YxNewsService;
import co.yixiang.modules.news.service.dto.YxNewsDto;
import co.yixiang.modules.news.service.dto.YxNewsQueryCriteria;
import co.yixiang.modules.news.service.mapper.YxNewsMapper;
import co.yixiang.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author ycl
* @date 2020-08-13
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxNews")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxNewsServiceImpl extends BaseServiceImpl<YxNewsMapper, YxNews> implements YxNewsService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YxNewsQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxNews> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxNewsDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YxNews> queryAll(YxNewsQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxNews.class, criteria));
    }


    @Override
    public void download(List<YxNewsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxNewsDto yxNews : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("订单id", yxNews.getObjectId());
            map.put("消息类型 1-聊天消息 2-系统消息 3-账户消息 4-物流消息 5-订单消息", yxNews.getType());
            map.put("消息图片", yxNews.getImage());
            map.put("消息标题", yxNews.getTitle());
            map.put("消息简介", yxNews.getSynopsis());
            map.put("消息内容", yxNews.getContent());
            map.put("创建时间", yxNews.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
